package com.venn.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by ut on 16-10-11.
 */
public class AddPartition {
    static SimpleDateFormat sdf1=new SimpleDateFormat("yyyyMMdd");
    static SimpleDateFormat sdf2=new SimpleDateFormat("yyyy-MM-dd");
    static Calendar cal=Calendar.getInstance();
    static Calendar cal1=Calendar.getInstance();



    public static String addPartition(String tableName,String partitionFix,String initial, String next,String startTime,String endTime){
        StringBuilder sb=new StringBuilder();
        try {
            Date startDate=sdf1.parse(startTime);
            Date endDate=sdf1.parse(endTime);
            long dayDiff=(endDate.getTime()-startDate.getTime())/(3600*24*1000);
            cal.setTime(startDate);
            cal1.setTime(startDate);
            cal1.add(cal1.DATE,1);
            for(int i=0;i<=dayDiff;i++){
                cal.add(cal.DATE,1);
                Date d=cal.getTime();
                cal1.add(cal1.DATE,1);
                Date e=cal1.getTime();
                String timeFix=sdf1.format(d);
                String timeFix1=sdf2.format(e);
                String partitionStr="alter table "+tableName+" add partition"+"\t\n"+
                        partitionFix+"_"+timeFix+
                        " values less than (TO_DATE('"+
                        timeFix1+" 00:00:00"+
                        "', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIAN'))"+"\t\n"+
                        "  tablespace WACOSOSS"+"\t\n"+
                        "  pctfree 10"+"\t\n"+
                        "  initrans 1"+"\t\n"+
                        "  maxtrans 255"+"\t\n"+
                        "  storage" +"\t\n"+
                        "    (" +"\t\n"+
                        "      initial "+initial+"M" +"\t\n"+
                        "      next "+next+"M" +"\t\n"+
                        "      minextents 1" +"\t\n"+
                        "      maxextents unlimited" +"\t\n"+
                        "      pctincrease 0" +"\t\n"+
                        "    );";
                sb.append(partitionStr+"\t\n");
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return sb.toString();
    }

}